package com.ruoyi.web.users;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.document.domain.DocBaseBank;
import com.ruoyi.document.domain.DocHighBank;
import com.ruoyi.document.domain.DocInformationsBank;
import com.ruoyi.document.service.IDocBaseBankService;
import com.ruoyi.document.service.IDocHighBankService;
import com.ruoyi.document.service.IDocInformationsBankService;
import com.ruoyi.users.vo.HistoryVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.users.domain.UHistories;
import com.ruoyi.users.service.IUHistoriesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 历史记录Controller
 *
 * @author Amy
 * @date 2024-12-03
 */
@RestController
@RequestMapping("/users/histories")
public class UHistoriesController extends BaseController
{
    @Autowired
    private IUHistoriesService uHistoriesService;
    @Autowired
    private IDocBaseBankService iDocBaseBankService;
    @Autowired
    private IDocHighBankService iDocHighBankService;
    @Autowired
    private IDocInformationsBankService iDocInformationsBankService;

    /**
     * 查询历史记录列表
     */
    @PreAuthorize("@ss.hasPermi('users:histories:list')")
    @GetMapping("/list")
    public TableDataInfo list(UHistories uHistories)
    {
        startPage();
        ArrayList<HistoryVo> historyVos = new ArrayList<>();
        List<UHistories> list = uHistoriesService.selectUHistoriesList(uHistories);
        for (UHistories histories : list) {
            HistoryVo historyVo = new HistoryVo();
            Long aLong = histories.getbId();
            Long aLong1 = histories.gethId();
            Long aLong2 = histories.getiId();
            if(aLong!=null){
                DocBaseBank docBaseBank = iDocBaseBankService.selectDocBaseBankById(aLong);
                String name = docBaseBank.getName();
                historyVo.setTitle(name);
                historyVo.setCreateTime(histories.getCreateTime());
                historyVo.setCreateBy(histories.getCreateBy());
                historyVo.setUpdateBy(histories.getUpdateBy());
                historyVo.setRemark(histories.getRemark());
                historyVo.setId(histories.getId());
                historyVos.add(historyVo);
            }
           if(aLong1!=null){
                DocHighBank docHighBank = iDocHighBankService.selectDocHighBankById(aLong1);
                String name = docHighBank.getName();
                historyVo.setTitle(name);
                historyVo.setCreateTime(histories.getCreateTime());
                historyVo.setCreateBy(histories.getCreateBy());
                historyVo.setUpdateBy(histories.getUpdateBy());
                historyVo.setRemark(histories.getRemark());
                historyVo.setId(histories.getId());
                historyVos.add(historyVo);
            }
            if(aLong2!=null){
                DocInformationsBank docInformationsBank = iDocInformationsBankService.selectDocInformationsBankById(aLong2);
                String name = docInformationsBank.getName();
                historyVo.setTitle(name);
                historyVo.setCreateTime(histories.getCreateTime());
                historyVo.setCreateBy(histories.getCreateBy());
                historyVo.setUpdateBy(histories.getUpdateBy());
                historyVo.setRemark(histories.getRemark());
                historyVo.setId(histories.getId());
                historyVos.add(historyVo);
            }


        }

        return getDataTable(historyVos);
    }

    /**
     * 导出历史记录列表
     */
    @PreAuthorize("@ss.hasPermi('users:histories:export')")
    @Log(title = "历史记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UHistories uHistories)
    {
        List<UHistories> list = uHistoriesService.selectUHistoriesList(uHistories);
        ExcelUtil<UHistories> util = new ExcelUtil<UHistories>(UHistories.class);
        util.exportExcel(response, list, "历史记录数据");
    }

    /**
     * 获取历史记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('users:histories:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(uHistoriesService.selectUHistoriesById(id));
    }

    /**
     * 新增历史记录
     */
    @PreAuthorize("@ss.hasPermi('users:histories:add')")
    @Log(title = "历史记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UHistories uHistories)
    {
        return toAjax(uHistoriesService.insertUHistories(uHistories));
    }

    /**
     * 修改历史记录
     */
    @PreAuthorize("@ss.hasPermi('users:histories:edit')")
    @Log(title = "历史记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UHistories uHistories)
    {
        return toAjax(uHistoriesService.updateUHistories(uHistories));
    }

    /**
     * 删除历史记录
     */
    @PreAuthorize("@ss.hasPermi('users:histories:remove')")
    @Log(title = "历史记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(uHistoriesService.deleteUHistoriesByIds(ids));
    }
}
